package Queue;

import java.util.LinkedList;
import java.util.Queue;

public class QueueStack {}
class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (queue2.isEmpty() && queue1.isEmpty()) {
            queue1.offer(x);
        } else if (!queue1.isEmpty()) {
            queue1.offer(x);
        } else {
            queue2.offer(x);
        }
    }

    public int pop() {
        //空栈返回-1
        if (queue2.isEmpty() && queue1.isEmpty()) {
            return -1;
        } else if (!queue1.isEmpty()) {
            //将1的元素转移到2中
            while (queue1.size() != 1) {
                queue2.offer(queue1.poll());
            }
            //最后一个1的元素poll
            return queue1.poll();
        } else {
            while (queue2.size() != 1) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        }
    }

    public int top() {
        //空栈返回-1
        if (queue2.isEmpty() && queue1.isEmpty()) {
            return -1;
        } else if (!queue1.isEmpty()) {
            //将1的元素转移到2中
            while (queue1.size() != 1) {
                queue2.offer(queue1.poll());
            }
            //最后一个1的元素存储后poll给2
            int n = queue1.poll();
            queue2.offer(n);
            return n;
        } else {
            while (queue2.size() != 1) {
                queue1.offer(queue2.poll());
            }
            int n = queue2.poll();
            queue1.offer(n);
            return n;
        }
    }

    public boolean empty() {
        if (queue2.isEmpty() && queue1.isEmpty()) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        int n = myStack.top();
        System.out.println(n);
        myStack.pop();
        int m = myStack.top();
        System.out.println(m);
        boolean b = myStack.empty();
        System.out.println(b);
    }
}
